<?php

namespace App\Models\Edu;

use Illuminate\Database\Eloquent\Model;
use Laracasts\Presenter\PresentableTrait;

class Clase extends Model
{
	use PresentableTrait;

	protected $table = 'edu_classes';

    protected $guarded = array();

    protected $with = ['term','users','category'];

    protected $presenter = 'App\Http\Presenters\ClasePresenter';

    /**
     * 班级报名用户
     * @return [type] [description]
     */
    public function users()
    {
        return $this->hasMany('\App\Models\Edu\Tuser')->orderBy('status','desc');
    }

    /**
     * 已录取用户
     * @return [type] [description]
     */
    public function enrolled()
    {
        return $this->hasMany('\App\Models\Edu\Tuser')->where('status', 2)->orderBy('status','desc');
    }

    public function branch()
    {
        return $this->belongsTo('\App\Models\Mgt\Branch');
    }


    public function type()
    {
        return $this->belongsTo('\App\Models\Edu\Type');
    }


    public function term()
    {
        return $this->belongsTo('\App\Models\Edu\Term');
    }


    public function category()
    {
        return $this->belongsTo('\App\Models\Edu\Category');
    }

    /**
     * 班级花名册学生列表
     */
    public function students($publishId)
    {
        return $this->hasMany('\App\Models\Lst\Student')->where('publish_id', $publishId);
    }

    /**
     * 课程所属的发布计划，用于判断用户是否可以自主取消报名
     * 如果该计划未处于发布状态，则不允许取消
     * @return [type] [description]
     */
    public function deploy()
    {
        return $this->hasOneThrough(
            '\App\Models\Edu\Deploy',
            '\App\Models\Edu\Info',
            'clase_id',
            'id',
            'id',
            'deploy_id',
            );
    }

}